package cn.edu.glut.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.glut.dto.UserIntro;
/**
 * 
 * @author Caizhfy
 * @email caizhfy@163.com
 * @createTime 2017年12月7日
 * @description 基础登录拦截器
 *
 */
@Configuration
public class LoginInterceptor implements HandlerInterceptor{
	final Logger logger = LoggerFactory.getLogger(HandlerInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		// 如果用户为空，重定向登录页面
		UserIntro u = (UserIntro) session.getAttribute("user");
		if (null==u || null==u.getId() || null==u.getUserType()) {
			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
